/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.service.processor.partner;

import cn.hutool.core.lang.Assert;
import info.peigen.hotpot.business.account.facade.order.create.CreateIntermediateAccountOrder;
import info.peigen.hotpot.business.account.facade.result.create.CreateAccountResult;
import info.peigen.hotpot.business.customer.core.domain.partner.Partner;
import info.peigen.hotpot.business.customer.core.service.processor.base.AbstractCustomerServiceProcessor;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import org.noear.solon.annotation.Component;

/**
 * <b>(PartnerCreateIntermediateAccountProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/5/26
 */
@Component
public class PartnerCreateIntermediateAccountProcessor extends AbstractCustomerServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 52;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        Partner partner = (Partner) serviceContext.getDomain();
        CreateIntermediateAccountOrder createIntermediateAccountOrder = CreateIntermediateAccountOrder.builder()
                .partnerId(partner.getPartnerId())
                .aliasName("合作伙伴[" + partner.getPartnerName() + "]中间账户")
                .build();
        CreateAccountResult result = accountServiceFacade.createIntermediateAccount(createIntermediateAccountOrder);
        Assert.notNull(result, () -> new BusinessException(ResultCode.FAILURE, "创建[合作伙伴-中间账户]失败![partnerName=" + partner.getPartnerName() + "]"));
        Assert.isTrue(result.success(), () -> new BusinessException(ResultCode.FAILURE, "创建[合作伙伴-中间账户]失败![partnerName=" + partner.getPartnerName() + "]"));
        serviceContext.getResult().setSuccessNotFee();
    }
}
